<?php
namespace Admin\Controller;
use Think\Controller;

class CommonController extends HomeController{
	/*
	* Function:		uploadImage
	* Description:	图片上传
	*/
	public function uploadImage(){
		// $data = array();
		// //设置编码防止乱码
		// header("Content-Type:text/html;charset=utf-8");
		// $upload = new \Think\Upload();// 实例化上传类
		// $upload->maxSize   =	10485760;// 设置附件上传大小 10M
		// $upload->exts      =	array('jpg','jpeg','gif','png','doc','docx','pdf','zip','rar','7z','xls','xlsx');// 设置附件上传类
		// $upload->savePath  =	'/image/'; // 设置附件上传目录
		// $upload->replace   =	true; //存在同名文件是否是覆盖，默认为false
		// //上传文件
		// $info = $upload->uploadOne($_FILES['file']);
		// $filename = 'https://'.$_SERVER["SERVER_NAME"].'/Uploads'.$info['savepath'].$info['savename'];
		// $exts = $info['ext'];
		// if(!$info) {// 上传错误提示错误信息
		// 	//$this->error($upload->getError());
		// 	$data['code'] = 0;
		// 	$data['value'] = '上传失败:'.$upload->getError();
		// }else{//上传成功
		// 	$data['code'] = 1;
		// 	$data['value'] = $filename;
		// }
		$data = array();
        if(empty($_FILES['file']['tmp_name'])){
            $data['code'] = 0;
            $data['value'] = '请上传文件';
            $this->ajaxReturn($data);
        }
        if($_FILES['file']['size']>1024*1024){
            $data['code'] = 0;
            $data['value'] = '上传文件不能超过1MB';
            $this->ajaxReturn($data);
        }
        $ext = explode('.', $_FILES['file']['name']);
        $ext = end($ext);//
        $exts = array('jpg','jpeg','gif','png');
        if(!in_array($ext,$exts)){
            $this->ajaxReturn(['code'=>0,'value'=>'上传的文件类型不对']);
        }
        $ret = D('Qiniu')->uploadFile($_FILES['file']['tmp_name'],$ext);
        if($ret['code']==1){
            $data['code'] = 1;
            $data['value'] = $ret['fileUrl'];
        }else{
            $data['code'] = 0;
            $data['value'] = $ret['errorMsg'];
        }
        $this->ajaxReturn($data);
	}

	/*
	* Function:		uploadImage{"code": 200, "data": {"url": "xxx"}, "msg": ""}
	* Description:	图片上传
	*/
	public function uploadImage2(){
		$data = array();
		//设置编码防止乱码
		header("Content-Type:text/html;charset=utf-8");
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize   =	10485760;// 设置附件上传大小 10M
		$upload->exts      =	array('jpg','jpeg','gif','png','doc','docx','pdf','zip','rar','7z','xls','xlsx');// 设置附件上传类
		$upload->savePath  =	'/image/'; // 设置附件上传目录
		$upload->replace   =	true; //存在同名文件是否是覆盖，默认为false
		//上传文件
		$info = $upload->uploadOne($_FILES['file']);
		$filename = 'https://'.$_SERVER["SERVER_NAME"].'/Uploads'.$info['savepath'].$info['savename'];
		$exts = $info['ext'];
		if(!$info) {// 上传错误提示错误信息
			//$this->error($upload->getError());
			$data['code'] = 0;
			$data['msg'] = '上传失败:'.$upload->getError();
		}else{//上传成功
			$data['code'] = 200;
			$data['data']['url'] = $filename;
			$data['msg'] = '上传成功';
		}
		$this->ajaxReturn($data);
	}
	
	/*
	* Function:		uploadDocFile
	* Description:	word文件上传
	*/
	public function uploadDocFile(){
		$data = array();
		//设置编码防止乱码
		header("Content-Type:text/html;charset=utf-8");
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize   =	104857600;// 设置附件上传大小 10M
		$upload->exts      =	array('doc','docx');// 设置附件上传类
		$upload->savePath  =	'/file/'; // 设置附件上传目录
		$upload->replace   =	true; //存在同名文件是否是覆盖，默认为false
		//上传文件
		$info = $upload->uploadOne($_FILES['file']);
		$filename = '/Uploads'.$info['savepath'].$info['savename'];
		$exts = $info['ext'];
		if(!$info) {// 上传错误提示错误信息
			//$this->error($upload->getError());
			$data['code'] = 0;
			$data['value'] = '上传失败:'.$upload->getError();
		}else{//上传成功
			$data['code'] = 1;
			$data['value'] = $filename;
		}
		$this->ajaxReturn($data);
	}
	
	/*
	* Function:		uploadOtherFile
	* Description:	文件上传
	*/
	public function uploadOtherFile(){
		$data = array();
		//设置编码防止乱码
		header("Content-Type:text/html;charset=utf-8");
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize   =	104857600;// 设置附件上传大小 10M
		$upload->exts      =	array('jpg','jpeg','gif','png','doc','docx','xls','xlsx','pdf','pdf','zip','rar','7z');// 设置附件上传类
		$upload->savePath  =	'/otherfile/'; // 设置附件上传目录
		$upload->replace   =	true; //存在同名文件是否是覆盖，默认为false
		//上传文件
		$info = $upload->uploadOne($_FILES['file']);
		$filename = '/Uploads'.$info['savepath'].$info['savename'];
		$exts = $info['ext'];
		if(!$info) {// 上传错误提示错误信息
			//$this->error($upload->getError());
			$data['code'] = 0;
			$data['value'] = '上传失败:'.$upload->getError();
		}else{//上传成功
			$data['code'] = 1;
			$data['value'] = $filename;
		}
		$this->ajaxReturn($data);
	}
	
	/*
	* Function:		getGoodsInfo
	* Description:	获取商品信息
	*/
	public function getGoodsInfo(){
	    $goods_id = I('goods_id/d','');
	    if(!empty($goods_id)){
	        $info = D('Goods')->getGoodsInfo($goods_id);
	        $this->ajaxReturn(['code'=>1,'data'=>$info]);
	    }
	    $this->ajaxReturn(['code'=>0,'message'=>'不存在此商品']);
	}
}